!**************************************************************************
!* This routine verifies wiring from the Bed-of-Nails fixture to the      *
!* Universal Station Virginia Panel Interface.  The program pulses a signal
!* out to the Vector I/O module.  The program indicates which Vector I/O  *
!* is connected to the pin being probed.                                  *
!* This program only checks the  Vector I/O selected by the Operator.     *
!*                                                                        *
!**************************************************************************
! Bob Christopherson          7/21/91
! Called by: UNI_MENU,UNI_SUB
! Location : SELF_DIAG Directory
!
!******************  PROGRAM UPDATES  *************************************
! DATE     CHANGES
! -------  ----------------------------------------------------------------
! 8-3-91   Added Multiple module tests.
! 8-8-91   Added detailed comments.
!
!**************************************************************************

program uni_prbe

  declare
    global numeric t1i                   ! ** keyboard input            **
    global numeric win2                  ! ** Test Window               **
    global numeric win1                  ! ** List of Tests window      **
    persistent numeric cntrl_all
    global numeric t1o

    global string array[1:4] result      ! ** list of I/O pins          **
    global numeric terminate             ! ** subroutine flag to        **
                                         ! ** stop the program          **

    string iom1 = "/mod1"
    string iom2 = "/mod2"
    string iom3 = "/mod3"
    string iom4 = "/mod4"
    string array [1:4] io_mod            ! ** use array to loop thru   **
                                         ! ** the Vector I/O's         **
    string low_word = "0000000000000000000000000000000000000000"
    string tri_state = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    string temp_string
    string module_no ="1"
    string active_modules                ! ** use to arm all 4 modules **
                                         ! ** at one time              **
    string char

    global string clr_scrn = "\1B[2J"
    global string cur_on = "\1B[?25h"
    global string cur_off = "\1B[?25l"
    global string rev = "\1B[7m"
    global string norm = "\1B[0m"
    global string bold = "\1B[1m"
    global string lf_on = "\1B[20h"
    global string lf_off = "\1B[20l"

    global numeric array[1:4] io_enable  ! 1 = Vector I/O connected.
    numeric temp_fflag                   ! Track Multiple failures
    numeric fflag                        ! Failure Flag to return
    numeric rcv_pin                      ! pin 1-40 being checked
    numeric io                           ! step thru Vect. I/O 1 to 4
                                         ! Operator Selects Module
                                         !  to test.
    numeric temp                         ! temporary storage.
    numeric debug_it = 0                 ! Set to 1 for debugging.
    numeric end_test                     ! End of Test flag.
    numeric temp_count                   ! Tracking for NO SIGNAL
    numeric button_status                ! Status of button probe
    numeric io_status                    ! Result of checkstatus command
    numeric status_flag                  ! Set if checkstatus > 1

  end declare

  ! @@@@@@@@@@@@@@  Begin - Print Connections  @@@@@@@@@@@@@@@@@@@@@@@@@@
  ! @                                                                   @
  ! @ Test Window displays Vector I/O pins listed in the result array.  @
  ! @ Only displays Vector I/O's selected by the io_enable array.       @
  ! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  function print_info

  declare

    global string array[1:4] result       ! String indicates 40 pins
    global numeric array[1:4] io_enable   ! 1= Vector I/O connected
    global numeric t1i
    global numeric win2

    global string clr_scrn = "\1B[2J"

    numeric io                            ! I/O counter
    numeric temp
    numeric temp_row

    string array[1:2] temp_string

  end declare

    print using clr_scrn,on win2
    winctl channel win2, position "unhide"

    for x = 1 to 15        ! ** draw shaded bar across top of window **
      print on win2, "\07"
    next

    print using "\1B[1;2HVECTOR I/O PIN CONNECTIONS",on win2
    print using "\1B[2;2HPush the probe button to measure the I/O",on win2
    print using "\1B[3;2Hor push the CLEAR button to STOP the test.",on win2
    print using "\1B[4;3HProbe is connected to the following pins",on win2
    print using "\1B[5;3Hon Vector I/O ",on win2
    print using "\1B[6;3H      \1B[7;3H",on win2


    ! ** Vector I/O pin connections - Display each I/O Status **
    temp = 8
    for io = 1 to 4
      if io_enable[io] = 1 then
        print using "\1B[" + (str(temp,10)) + ";3HI/O ?#= ?# \1B[" + (str(temp+1,10)) + ";3H",on win2, io, result[io]
        temp = temp + 1              ! ** increment display row
      end if                         ! ** if io_enable[]=1
    next                             ! ** next io 1-4

!    winctl channel win2, position "hide"

    end function
! @@@@@@@@@@@@@@@@@  end - print failures  @@@@@@@@@@@@@@@@@@@@@@@@@@@@

! @@@@@@@@@@@@@  Begin - Select Vector I/O Module  @@@@@@@@@@@@@@@@@@@@
! @                                                                   @
! @  Operator toggles the io_enable flag array ON & OFF for each      @
! @  Vector I/O Module                                                @
! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  function Select_number

  declare

    global numeric array[1:4] io_enable   ! 1 = Vector I/O connected.
    global numeric t1i                    ! keyboard input
    global numeric win2                   ! Test/Menu window
    global numeric win1                   ! List of Tests window
    global numeric t1o                    ! output to keyboard(unbuff)
    global numeric terminate              ! abort program
    global string clr_scrn = "\1B[2J"

    numeric temp
    string temp_string
    string input_string   = ""            ! 'readmenu' input

  end declare

    winctl channel win1, position "hide"   ! ** close list of tests window **
    print using clr_scrn,on win2
    winctl channel win2, position "unhide" ! ** open test window           **

    for x = 1 to 15
      print on win2, "\07"
    next

    print using "\1B[1;2HMODULE SELECTION",on win2
    print using "\1B[2;2H",on win2

    print using "\1B[3;4HAll Vector I/O Modules default to ON.",on win2
    print using "\1B[4;4HSelect the I/O number to turn module OFF.",on win2
    print using "\1B[5;4HSelect the I/O number again (toggles)to turn module ON.",on win2
    print using "\1B[6;4HPress TEST when ready.  \1B[8;4H",on win2
    print using "\1B[8;4HI/O 1     I/O 2     I/O 3     I/O 4  \1B[9;4H",on win2


    ! *******  Select I/O Numbers until F5 is selected  ***************

    define menu "M1-a", label "I/O 1", key "1"
    define menu "M1-b", label "I/O 2", key "2"
    define menu "M1-c", label "I/O 3", key "3"
    define menu "M1-d", label "I/O 4", key "4"
    define menu "M1-e", label "TEST", key "5"
    define menu "M1-f", label "QUIT", key "6"
    input_string = ""
    for i = 1 to 4                 ! ** all I/O modules enabled     **
        io_enable[i] = 1
    next

    ! ********  Select Vector I/O to enable   ************************

    loop until (input_string = "M1-e" or input_string = "M1-f")
           ! ** loop until QUIT or TEST button selected            **
           ! ** setup temp_string to display enable/disable status **

     temp_string = ""
     if io_enable[1] then
        temp_string = " ON       "
     else
        temp_string = " OFF      "
     end if
     if io_enable[2] then
        temp_string = temp_string + " ON       "
     else
        temp_string = temp_string + " OFF      "
     end if
     if io_enable[3] then
        temp_string = temp_string + " ON       "
     else
        temp_string = temp_string + " OFF      "
     end if
     if io_enable[4] then
        temp_string = temp_string + " ON       "
     else
        temp_string = temp_string + " OFF      "
     end if

     print using "\1B[9;4H?#  \1B[10;4H",on win2,temp_string

     ! *****  INPUT selection from menu into input_string  ***********

     input_string = readmenu channel t1i, identifier "M1", style 0, xorg 0, yorg 5
       if input_string = "M1-a" then ! ** enable/disable I/O 1      **
         if (io_enable[1] = 1) then
            io_enable[1] = 0
         else
            io_enable[1] =1
         end if                      ! ** end if (io_enable[]=1)    **
       end if                        ! ** end if input_string="M1-  **

       if input_string = "M1-b" then ! ** enable/disable I/O 2      **
         if (io_enable[2] = 1) then
            io_enable[2] = 0
         else
            io_enable[2] =1
         end if                      ! ** end if (io_enable[]=1)    **
       end if                        ! ** end if input_string="M1-b **

       if input_string = "M1-c" then ! ** enable/disable I/O 3      **
         if (io_enable[3] = 1) then
            io_enable[3] = 0
         else
            io_enable[3] =1
         end if                      ! ** end if (io_enable[]=1)    **
       end if                        ! ** end if input_string="M1-c:**

      if input_string = "M1-d" then  ! ** enable/disable I/O 4      **
         if (io_enable[4] = 1) then
            io_enable[4] = 0
         else
            io_enable[4] =1
         end if                      ! ** end if (io_enable[]=1)    **
       end if                        ! ** end if input_string="M1-d **

 !    end if                         ! ** end if poll t1i           **

    end loop              ! ** end loop until input_string = "M1-e" **

    if (input_string = "M1-f") then          ! ** QUIT key selected **
      active_modules = ""
      terminate = 1                          ! ** terminate flag    **
      remove menu "M1"
      return(active_modules)                 ! ** END PROGRAM  *******
    end if

 ! *********  setup single variable to select all modules  ***********

    active_modules =""                     ! ** used in arm command **
                                           ! ** ie., ="/mod1,/mod2" **
    if io_enable[1] then active_modules = "/mod1,"

    if io_enable[2] & active_modules = "" then
          active_modules = "/mod2"
    else if io_enable[2] & active_modules <> "" then
          active_modules = active_modules + "/mod2,"
    end if

    if io_enable[3] & active_modules = "" then
          active_modules = "/mod3"
    else if io_enable[3] & active_modules <> "" then
          active_modules = active_modules + "/mod3,"
    end if

    if io_enable[4] & active_modules = "" then
          active_modules = "/mod4"
    else if io_enable[4] & active_modules <> "" then
          active_modules = active_modules + "/mod4"
    end if
           ! ***** active_modules = "/mod1,/mod2,etc."  ******
  ! ********  remove extra comma from end of active_modules  *********

    temp_string = ""
    temp = len active_modules               ! ** comma as last char  **
    temp_string = mid(active_modules,temp,1)
    if (temp_string = ",") then             ! ** if last char = comma *
       temp_string = mid(active_modules,1,temp-1)
       active_modules = temp_string
    end if
    winctl channel win2, position "hide"   ! ** hide the Test window **

    return(active_modules)

    end function

! @@@@@@@@@@@@@@  end - Select Vector I/O Modules   @@@@@@@@@@@@@@@@@@


! @@@@@@@@@@@@@@@@@@@@@  BEGIN MAIN PROGRAM  @@@@@@@@@@@@@@@@@@@@@@@@@
! @ This section calls 'select_number' to disable or enable the      @
! @ Vector I/O's.  The program loops until the probe button or the   @
! @ clear button (stops the test) are pressed.                       @
! @ Each of the 40 pins on the selected Vector I/O Modules are       @
! @ checked to verify that pulsing was received from the probe.      @
! @                                                                  @
! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  ! *****   Vector I/O Module array **********************************

    io_mod[1] = "/mod1"
    io_mod[2] = "/mod2"
    io_mod[3] = "/mod3"
    io_mod[4] = "/mod4"

    terminate = 0                          ! ** initialize for sel_num**
    ! *****   Enable Vector I/O Modules  ********************************
    active_modules = Select_number() ! ** SUBROUTINE -  Enter I/O #   ***
    ! *****   End program if requested in Select_number() ***************
    if terminate then                      ! ** stop test selected    ***
     remove menu "M1"
     winctl channel win1, position "unhide"! ** open list of test window **
     winctl channel win2, position "hide"  ! ** close I/O test window    **
        return("PASSED")                   ! ** in Select_number      ***
    end if
    ! *****   Initialize Vector I/O Modules  ****************************
    reset device active_modules
    for io = 1 to 4                        ! ** Clear Vector I/O's    ***
     if io_enable[io] then
       reset (io_mod[io])
       sync (io_mod[io],"freerun")
       sync ("/probe","freerun")
       counter (io_mod[io],"transition")
     end if
    next

! ************   Set Vector I/O Outputs to Tri-state  *******************
  setword device iom1, word 1, as_pins "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40"

  setword device iom2, word 2, as_pins "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40"

  setword device iom3, word 3, as_pins "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40"

  setword device iom4, word 4, as_pins "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40"

  writeword device iom1, word 1,patt tri_state
  writeword device iom2, word 2, patt tri_state
  writeword device iom3, word 3, patt tri_state
  writeword device iom4, word 4, patt tri_state

! **************   initialize flags/variables    *********************
  fflag = 0
  temp_flag = 0

  for i = 1 to 4                         ! ** Initialize result     **
   result[i] = ""
  next
  print_info()             ! ** SUBROUTINE -  Display test window   **

! ********************************************************************
! * Runs Test each time operator presses the PROBE BUTTON.           *
! ********************************************************************

end_test = 0
loop until (end_test)                    ! ** Beginning of test loop *
for i = 1 to 4
   result[i] = ""
next

      ! ********  open operator display  ************************

   print on t1o, "PROBE PIN AND PUSH BUTTON ON PROBE!"

      ! ********  loop for probe or stop key here  **************

   char = ""
   button_status =0
   loop until button_status = 1 or char = chr($7F)
      button_status = pollbutton()             ! ** probe button pressed ??

      if button_status = 1 then                ! ** button was pressed !!
        readbutton()
      end if
      char = ""
      if (poll channel t1i, event "input") then
        input on t1i, char
        if char = chr($7F) then                ! ** ABORT test            **
          end_test = 1
        end if
      end if                                   ! ** end if poll channel t1i
   end loop                                    ! ** end loop button_status=1

 ! ***********  Send and Measure Pulses  *****************************

    for io = 1 to 4                        ! ** Clear Vector I/O's    ***
     if io_enable[io] then
       sync (io_mod[io],"ext")
       edge (io_mod[io],"+","+","+")
       enable (io_mod[io],"high")
     end if
    next

  arm (active_modules)
     pulser mode "high"                  ! pulse low to high
   ! loop while ((checkstatus dev
    wait (2000)
  readout (active_modules)              ! show i/o mod captured responses


 ! ***********  Measure Start,Stop,Enable  ***************************

  for io = 1 to 4                       ! ** check each Vector I/O  **
   if io_enable[io] then
    io_status = checkstatus device io_mod[io]
       if (io_status = setbit 1) then
          result[io] = result[io] + "ENABLE"     ! ** detected Enable
       else if (io_status = setbit 2) then
          result[io] = result[io] + "START"      ! ** detected Start
       else if (io_status = setbit 3) then
          result[io] = result[io] + "STOP"       ! ** detected Stop
       end if                           ! ** end if io_status = setbit

   end if                               ! ** end if io_enable[io]   **
  next                                  ! ** next io (1-4)          **

 ! ***********  Send and Measure Pulses  *****************************

    for io = 1 to 4                        ! ** Clear Vector I/O's    ***
     if io_enable[io] then
       sync (io_mod[io],"freerun")
     end if
    next

  arm (active_modules)
    pulser mode "high"                  ! pulse low to high
    wait (8)
  readout (active_modules)              ! show i/o mod captured responses


 ! **********  Check 40 pins on each Enabled Vector I/O **************

   cnt = 0                                ! ** number of pulses      *
   temp_count = 1                         ! ** Show NO SIGNAL if no  *
                                          ! ** counts occur.         *
   for io = 1 to 4                        ! ** check each Vector I/O *
   if io_enable[io] then
   temp_count = 1                         ! ** set NO SIGNAL flag   **
   for rcv_pin = 1 to 40                  ! ** check each Receive pin*

    cnt = count device io_mod[io], pin rcv_pin

    if (cnt > 4) then                     ! ** less than 4 may be glitchs **
     temp_fflag = 1
     temp_count = 0                       ! ** clear NO SIGNAL flag  **
     if result [io] = "" then             ! ** first entry into result*
        result [io] = str(rcv_pin,10)
     else
        result [io] = result [io] + (str(rcv_pin,10)) + ", "
        ! **  add connecting pin number to string **
     end if                            ! ** end if rcv_pin = 1      ***

     !if (len result[fflag] > 25) then
        !fflag = fflag + 1
     !end if                           ! ** end if (len result[temp]***
    end if                             ! ** end if cnt>0            ***

    next                               ! ** next Receiving Pin      ***

   if temp_count = 1 then result[io] = "NO SIGNAL RECEIVED"

   end if                              ! ** end if io_enable = 1    ***

next                                   ! ** for io = 1-4            ***
   print_info()          ! ** SUBROUTINE - Display Pins Numbers     ***

end loop                          ! ** end of loop until (end_test) ***

winctl channel win1, position "unhide" ! ** open list of test window **
winctl channel win2, position "hide"   ! ** close I/O test window    **
remove menu "M1"

  temp_fflag = 1
  if (temp_fflag > 0) then
    return("PASSED")                   ! ** found a pin               **
  else
    return("FAILED")                   ! ** did not find a pin        **
  endif


if (debug_it) then
       close (t1o)
       close (t1i)
end if

end program


